package com.hutao.manage.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.manage.pojo.LeftMenuPojo;
import com.hutao.manage.pojo.RespondResult;
import com.hutao.manage.query.MedBindQuery;
import com.hutao.manage.service.UploadScanService;
import com.hutao.manage.util.CookieUtils;
import com.hutao.manage.util.JsonUtils;

@Controller
@RequestMapping("/page")
public class PageController {
	private static final Logger log = LoggerFactory.getLogger(PageController.class);

	@RequestMapping("/index")
	public String index(){
		return "index.index";
	}
	@RequestMapping("/print")
	public String print(){
		return "print";
	}
	
	@RequestMapping("/print2")
	public String print2(){
		return "print2";
	}
	
	@RequestMapping("/print3")
	public String print3(){
		return "print3";
	}
	
	//跳转到文件上传
	@RequestMapping("/uploadify")
	public String uploadify(@ModelAttribute("errorCode") String errorCode, @ModelAttribute("msg") String msg, Model model, HttpServletRequest request){
		LeftMenuPojo leftMenu = LeftMenuPojo.GetLeftMenu("scan_upload");
		String leftMenuJson = JsonUtils.objectToJson(leftMenu);
		model.addAttribute("leftMenuJson", leftMenuJson);
		model.addAttribute("errorCode", errorCode);
		model.addAttribute("msg", msg);
		
		
		return "upload/uploadify.page";
	}
	
	//跳转到文件上传
	@GetMapping("/uploadify_query_page")
	public String uploadifyQueryPage(Model model,HttpServletRequest request){
		
		LeftMenuPojo leftMenu = LeftMenuPojo.GetLeftMenu("scan_upload");
		String leftMenuJson = JsonUtils.objectToJson(leftMenu);
		model.addAttribute("leftMenuJson", leftMenuJson);
		String userCode = CookieUtils.getCookieValue(request, "TT_TOKEN");
		model.addAttribute("userCode", userCode);
		return "upload/uploadify_query_page.page";
	}
	
	@Autowired
	private UploadScanService uploadScanService;
	
	@GetMapping("/ajax_query_uploadify")
	@ResponseBody
	public RespondResult ajaxQueryMedicalRecordOfPageBorrow(MedBindQuery params,HttpServletRequest request){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> records = new ArrayList<Map<String,Object>>();
			int count = 0;
			if(!params.isEmpty()){
				String currentUser = CookieUtils.getCookieValue(request, "TT_TOKEN");
				records = uploadScanService.getUploadScanByMedBindQuery(params);
				count = uploadScanService.getUploadScanCountByMedBindQuery(params);
			}
			
			params.setTotalCounts(count);
			params.setDatas(records);
			respondResult = new RespondResult(true, RespondResult.successCode, null, params);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), params);
			e.printStackTrace();
		}
		
		return respondResult;
	}
	
	//病案绑定
	@RequestMapping("/medBind")
	public String medBind(){
		return "medbind/Pigeonhole_list";
	}
	
	//病案绑定
	@RequestMapping("/pigeonHoleList")
	public String pigeonHoleList(){
		return "medbind/Pigeonhole_bind";
	}
}
